Conditionally display field based on comparison to other field
I finally solved the problem by adding a template for the content type. This allowed me to simplify the preprocess function:

function mytheme_preprocess_node__event(&amp;$variables) {
  $startdate = $variables['content']['field_eventdate'][0]['#markup'];
  $enddate = $variables['content']['field_eventend'][0]['#markup'];

  if ($startdate === $enddate) {
    unset($variables['content']['field_eventend']);
  }
}


I then copied the node.html.twig template to node--event.html.twig. I only changed the main {{ content }} part into:

    {{ content.field_tags }}
    {{ content.field_eventlocation }}
    &lt;div class="event_dates clearfix"&gt;
      {{ content.field_eventdate }}
      {% if content.field_eventend %}
        &amp;nbsp;&amp;ndash; {{ content.field_eventend }}
      {% endif %}
    &lt;/div&gt;
    {{ content|without('field_tags', 'field_eventlocation', 'field_eventdate', 'field_eventend') }}



This replaced my attempt to create and modify field--field-eventdate.html.twig template.

Maybe this is not the best option, but at least it works.