How do I send a variable from PHP to JavaScript from a function that returns a render array?
It does apply to the method you are using. You can add metadata like #cache or #attached to any render element (#theme, #type, #markup, #plain_text):
return [
  '#theme' =&gt; 'theme_name',
  '#form' =&gt; $form,
  '#data' =&gt; [
    'customer' =&gt; $obj_customer,
    'customer_subscription' =&gt; $obj_customer-&gt;getSubscription(),
    'subscriptions_explanation' =&gt; $arr_subscription_text,
  ],
  '#attached' =&gt; ['drupalSettings' =&gt; ['do' =&gt; $something]],
];