Mixing HTML with variables is common practice when developing custom applications. This snippet shows how to mix HTML and variables the correct way.
use Drupal\Component\Render\FormattableMarkup;
$output = new FormattableMarkup('<h2>@title</h2>', ['@title' => $entity->label()]);
More about the FormattableMarkup class here.