Show category name in category.php when posts assigned to multiple categories

For the heading on a category archive use either:

the_archive_title();

Or:

single_term_title();

The first one can be used on the template for any archive, while the second should only be used on category, tag, or taxonomy archive templates.

You shouldn’t need to get anything from any of the individual posts. They may have other categories and the category relevant to the current archive might not be the most relevant.