Drupal - How to set a default theme programmatically
Looking at ThemeController::setDefaultTheme
(the request handler for the /admin/appearance/default route), this should do the trick:
\Drupal::configFactory()
->getEditable('system.theme')
->set('default', 'machine_name')
->save();