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.


Saved you some valuable time?

Donate and get more free time-saving code on this website in the future.