Replace placeholder in custom block with preprocess hook
In addition to @sonfd answer you also need to consider caching. The easiest way would be to add a cache context:
$variables['#cache']['contexts'][] = 'user';

But then the database needs to store a cached version for each user.
Better for performance would be to postpone replacing the placeholder by attaching a lazy builder to the render array:
function mytheme_preprocess_block(&amp;$variables) {
  $variables['#attached']['placeholders']['{{user-uid}}'] = [
    '#lazy_builder' =&gt; ['_mytheme_replace_uid', []],
  ];
}

which then returns the markup of the user UID:
function _mytheme_replace_uid() {
  return [
    '#markup' =&gt; \Drupal::currentUser()-&gt;id(),
    '#cache' =&gt; ['contexts' =&gt; ['user']],
  ];
}

Drupal 9
Drupal 9 requires using only trusted callbacks. So define a class implementing TrustedCallbackInterface or RenderCallbackInterface:
mytheme/src/MythemeLazyBuilders.php:
&lt;?php

namespace Drupal\mytheme;

use Drupal\Core\Render\Element\RenderCallbackInterface;

class MythemeLazyBuilders implements RenderCallbackInterface {

  public static function replaceUid() {
    return [
      '#markup' =&gt; \Drupal::currentUser()-&gt;id(),
      '#cache' =&gt; ['contexts' =&gt; ['user']],
    ];
  }

}

and add it to the render array:
function mytheme_preprocess_block(&amp;$variables) {
  $variables['#attached']['placeholders']['{{user-uid}}'] = [
    '#lazy_builder' =&gt; ['\Drupal\mytheme\MythemeLazyBuilders::replaceUid', []],
  ];
}