Wrapping content in anchor tags with CKEditor
I found a solution. As @Clive said CKEDITOR.dtd.a.h2 = 1; and CKEDITOR.dtd.a.h3 = 1; is the solution.
These need to be declared in a my_module.module file:
function my_module_editor_js_settings_alter(array &amp;$settings) {

  foreach (array_keys($settings['editor']['formats']) as $text_format_id) {
    if ($settings['editor']['formats'][$text_format_id]['editor'] === 'ckeditor') {
        $settings['editor']['formats'][$text_format_id]['editorSettings']['customConfig'] = 
        base_path() . drupal_get_path('module', 'my_module') . '/ckeditor_config.js';
}
  }
}

And then we create a file ckeditor_config.js:
CKEDITOR.dtd['a']['h2'] = 1;
CKEDITOR.dtd['a']['h3'] = 1;

Doing this we can wrap h2 and h3 elements with an anchor tag and CKEditor is not going to modify it.