Why is my library not attached when programmatically rendering a block?
I think it's because attached needs to be a separate element than your markup.

This is one render element for markup, with the attached attached to it:

$build = [
    '#attached' =&gt; [
      'library' =&gt; [
        'mymodule/mylibrary',
      ],
    ],
    '#markup' =&gt; '&lt;!-- see template --&gt;',
  ];


The correct way to set up a build array is with the markup being a separate element:

$build = [];

$build['#markup'] = ['#markup' =&gt; '&lt;h1&gt;testing 123&lt;/h1'];

$build['#attached']['library'] = ['mymodule/mylibrary'];

return $build;