diff -u b/core/lib/Drupal/Component/Utility/Html.php b/core/lib/Drupal/Component/Utility/Html.php
--- b/core/lib/Drupal/Component/Utility/Html.php
+++ b/core/lib/Drupal/Component/Utility/Html.php
@@ -90,6 +90,7 @@
$identifier = str_replace('__', '##', $identifier, $double_underscore_replacements);
}
$identifier = str_replace(array_keys($filter), array_values($filter), $identifier);
+ // Replace temporary placeholder '##' with '__' only if needed.
if ($double_underscore_replacements > 0) {
$identifier = str_replace('##', '__', $identifier);
}