Is there ever justification for adding HTML before passing content to a twig template? There seem to be many people trying to pass HTML markup into a twig template and having trouble doing it. It sounds like the best solution is to use something like:

$variables['test'] = ['#markup' =&gt; '&lt;strong&gt;some markup&lt;/strong&gt;'];


But, when would this be preferable (or necessary) over adding the markup in the twig template instead?
It's not possible to put all html markup in templates. Often for example in case of an error you need to generate some extra markup.

If it is a string constant like in your example this is no problem.

If you have variables, don't concatenate in markup.

Use Drupal\Component\Render\FormattableMarkup instead:

$variables['error']['#markup'] = new FormattableMarkup('&lt;strong&gt;Error @message at line @line&lt;/strong&gt;', [
  '@message' =&gt; $message,
  '@line' =&gt; $line,
]);


Most times you need to make this translatable and this is why t() is still one of the most used functions in drupal:

$variables['error']['#markup'] = t('&lt;strong&gt;Error @message at line @line&lt;/strong&gt;', [
  '@message' =&gt; $message,
  '@line' =&gt; $line,
]);


If you are in a class use $this-&gt;t().

To summarize, the answer to the question is yes, there is justification for adding html in php. This is, if you need to react dynamically on things happening in your code. Then instantiate a new FormattableMarkup object. If you need translation, you can simply use t().