toIn the above 'answer', the last line which prints the category name is not actually doing so.
It should look like this:
$categories = get_the_category();
foreach( $categories as $category) {
$name = $category->name;
$category_link = get_category_link( $category->term_id );
echo "<a href='$category_link'>
<span class=" . esc_attr( $name) . ">" . esc_attr( $name) . "</span>
</a>";
}