How do I get machine name by entity type and its ID?
To get the type and id of a given entity, you can do:

$type = $entity-&gt;bundle();
$id = $entity-&gt;id();


Some entities, like Node, provide wrapper methods to return the bundle:

$type = $node-&gt;getType();

But Block doesn't. So if it is Block that you are after:

$block = \Drupal\block_content\Entity\BlockContent::load(2);
$type = $block-&gt;bundle();
$id = $block-&gt;id();