How do I move conditional library includes out of a twig template?
Attaching libraries in Drupal 8 should be done by either attaching the #attached attribute to render arrays or by adding them to the info file to be added to all pages.

For example let's say I have a custom form class that needs my custom javascript and/or css files.

class MyForm extends FormBase {
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['#attached']['library'][] = 'my_module/my_library'
    return $form;
  }
}


If you want to attach a library to an element you didn't initiate then you will need to use an alter hook for that specific element.
If you want to attach a library based on other conditions then you can use the hook_page_attachments(array &amp;$attachments) function