Apply individual IDs on Headline inside Tag filtered loop

I’ve got three filtered ‘by tag’ categories. Each of these categories has a H2. I’m trying to create anchor links on the headline tags so I can add a menu at the top of the page, but can’t work it out. Below is what I’ve got so far. I hope that’s clear.

<h2 id="anchor-id-to-go-here"><?= $title ?></h2>
    <?php $articles = $page->children()
           ->filterBy('tags', $tagfilter, ',') ; ?>

<?php if($articles->count() > 0): ?>

      <?php foreach($articles as $article): ?>
        <a href="" target="_blank">
          <?= $article->image() ?>

        <a href="" target="_blank" class="section-link"><?= $article->title()->html() ?></a>

        <div>intu Watford – lower mall <br>
          01923 230760</div>
        <?= $article->text()->kirbytext() ?>

    <?php endforeach ?>
      <?php else: ?>
      <p>This blog does not contain any articles yet.</p>
<?php endif ?>

You could use str::slug() to sluggify the title:

<h2 id="<?= str::slug($title) ?>"><?= $title ?></h2>