You can use get_the_category( )
instead.
Example:
$categories = get_the_category();
foreach( $categories as $category) {
$name = $category->name;
$category_link = get_category_link( $category->term_id );
echo "<a href="https://wordpress.stackexchange.com/questions/249463/$category_link">
<span class=" . esc_attr( $name) . "></span>
</a>";
}