Add inline style to render array attributes, without XSS filtering
I'd use '#type' =&gt; 'inline_template' for this: You can do everything what you can do in a regular .twig file, just inlined in code:

  $build[0] = [
    '#type'       =&gt; 'inline_template',
    '#template'   =&gt; '&lt;div id="thumbnail-image" style="background-image: url({{ foo }})"&gt;{{ bar }}&lt;/div&gt;'
    '#context' =&gt; [
      'foo' =&gt; $youtube_thumbnail
      'bar' =&gt; $whatever
    ]
  ];
  return $build;


Here is a complete list of render elements / #type 's for render arrays. (For Forms API, here is the sub-list of FormRenderElements)