Rewrite markup of a field in twig template
Sorry I couldn't be more verbose earlier, I was typing from a phone.

One way to solve this would be to just create a new template variable. Also, its easier to use the field value instead of parsing the render array - the contents of that array could change without warning. Not saying it happens a lot, but is possible. Just access the field value directly in one of two ways:

$field_value = $node-&gt;get('field_name')-&gt;value;

or

$field_value = $node-&gt;get('field_name')-&gt;getValue();

Note that not all fields will work that way, but most basic ones like text or textlong do. Reference fields for example will work a little differently.

function mytheme_preprocess_node(&amp;$variables) {
  $node = $variables['node'];

  if ($node-&gt;getType() == 'mytype') {
    if (!$node-&gt;get('field_my_field')-&gt;isEmpty()) {
      $variables['new_variable'] = $node-&gt;get('field_my_field')-&gt;value;
    }
  }
}


Then, in your node--node-type.html.twig template of your theme, where you want the field output, do:

{{ new_variable }}

Note that if you use -&gt;getValue() above, you need to dig into that value array to get the actual value as opposed to -&gt;value.

Always isolate the effort to the node type(s) and templates it should affect. In your question, you say you have this:

function mytheme_preprocess_node(&amp;$variables) {
    $variables['content']['field_my_field'][0]['#markup'] = $newValue;
}


This code will fire for every node in the system any time it is loaded on that active theme. Unless that field exists on every single node type, it will cause a lot of warnings/notices in your logs. If you moved the field from visible to hidden from the admin UI, this code will fail as well, since the field will be removed from the content render array and not exist. I tend to also enforce checking both the node type and the view mode of that node being rendered too, but that is all up to what your design requirements are.