Why is Twig not outputting this correctly?
It's unsafe to use the |raw filter. Put HTML markup in a #markup element instead:

$variables['foo'] = [
  '#markup' =&gt; ' AA &lt;strong&gt; BB &lt;/strong&gt; CC ',
];


Then outputting this variable in Twig should work:

{{ foo }}