How to render a custom entity in a controller?
Controllers should return a render array (or a custom response), you almost never need to render anything yourself outside a template.

Just replace this:

$build = $view_builder-&gt;view($entity);
$render_array = render($build);
$output['entity'] = $render_array;


with:

$output = $view_builder-&gt;view($entity);


to return a render array and let Twig handle the rendering.

But going further, you don't need to (and shouldn't) go into the database directly for this; the entity type manager works for all entity types, including your own, and entity storage classes have methods to help with this. 

(As an aside, that service should also be injected rather than loaded from the static Drupal class method, your controller class is container aware). 

Once that's available the code would be simplified to something like this:

public function content($name) {
  $build = [];
  $entity_type = 'foo';

  $storage = $this-&gt;entityTypeManager-&gt;getStorage($entity_type);
  $entities = $storage-&gt;loadByProperties(['name' =&gt; $name]);

  if ($entity = reset($entities)) {
    $build = $this-&gt;entityTypeManager-&gt;getViewBuilder($entity_type)-&gt;view($entity);
  }

  return $build;
}


To go one step further, you can add a loadByName() method to your entity class with a slight variation of the above, might save you some time and repetition further down the line.