How to get the entity ID in a twig template
You can just add the full object to a twig variable, so
$vars['myentity'] = $my_entity;

and then access with
{{ myentity.id }}

A bit more elegant is to implement hook_theme:
/**
 * Implements hook_theme().
 */
function MODULENAME_theme() {
  return [
    'customentity' =&gt; [
      'render element' =&gt; 'elements',
    ],
  ];
}

and a preprocess function to expose it to twigs customentity and maybe content variables:
function template_preprocess_customentity(&amp;$variables) {
  $variables['customentity'] = $variables['elements']['#node'];

  // Helpful $content variable for templates.
  $variables += ['content' =&gt; []];
  foreach (Element::children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];
  }
}