To create extension attribute to product entity, I have followed the simple steps to make understandable for new magento 2 developer.

I selected ‘brand’ attribute name for the product entity.

Follow these steps to create extension attribute to product entity:

Step 1:

First to create module required file “\app\code\Coddu\ProductExtAttribute\etc\module.xml”

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
	<module name="Coddu_ProductExtAttribute" setup_version="1.0.0"/>
</config>

Step 2:

Next, create autoload file for module “\app\code\Coddu\ProductExtAttribute\registration.php”

<?php
/**
 * Copyright ©2020 Coddu.com, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Coddu_ProductExtAttribute', __DIR__);

Step 3:

Next, create extension_attributes.xml file to create attribte. Follow the file location “\app\code\Coddu\ProductExtAttribute\etc\extension_attributes.xml”.

<?xml version="1.0"?>
<!--
/**
 * Copyright ©2020 Coddu.com, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <extension_attributes for="Magento\Catalog\Api\Data\ProductInterface">
        <attribute code="brand" type="text" />
    </extension_attributes>
</config>

After creating this file, enable the module “Coddu_ProductExtAttribute” using this command “bin/magento module:enable Coddu_ProductExtAttribute” and compile the code using this command “bin/magento setup:di:compile”.

Next to find and open “ProductExtensionInterface” class on the similar location “root/generated/code/Magento/Catalog/Api/Data/ProductExtensionInterface.php”.

Find getBrand and setBrand methods in ProductExtensionInterface.php file. If new methods created, it means you have created new “brand” in extension_attributes successfully. Otherwise review the code.

/**
 * @return text|null
 */
public function getBrand();

/**
 * @param text $brand
 * @return $this
 */
public function setBrand(\text $brand);

Step 4:

To create a ‘brand’ attribute to product entity, I am creating a patch class. See the code below:

<?php
/**
 * Copyright ©2020 coddu.com, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Coddu\ProductExtAttribute\Setup\Patch\Data;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Catalog\Model\Product as ProductModel;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;

class AddProductBrandAttribute implements DataPatchInterface
{
    /**
     * @var ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * AddAlternativeColorAttribute constructor.
     * @param ModuleDataSetupInterface $moduleDataSetup
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $eavSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->eavSetupFactory = $eavSetupFactory;
    }

    /**
     * @return DataPatchInterface|void
     * @throws \Magento\Framework\Exception\LocalizedException
     * @throws \Zend_Validate_Exception
     */
    public function apply()
    {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);

        $eavSetup->addAttribute(ProductModel::ENTITY, 'brand', [
            'type' => 'text',
            'backend' => '',
            'frontend' => '',
            'label' => 'Brand',
            'input' => 'text',
            'group' => 'General',
            'sort_order' => 30,
            'class' => '',
            'source' => '',
            'global' => ScopedAttributeInterface::SCOPE_WEBSITE,
            'visible' => true,
            'required' => true,
            'user_defined' => false,
            'default' => '',
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => false,
            'used_in_product_listing' => true,
            'unique' => false,
        ]);
    }

    /**
     * @return array|string[]
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * @return array|string[]
     */
    public function getAliases()
    {
        return [];
    }
}

Run ‘php bin/magento setup:upgrade’ cli. It will create a ‘brand’ attribute in ‘eav_attribute’ table to product entity and add a new record in the ‘patch_list’ table with patch class path i.e. ‘Coddu\ProductExtAttribute\Setup\Patch\Data\AddProductBrandAttribute’.

Step 5:

Next create a ‘di.xml’ file to declares a plugin for a class object.

<?xml version="1.0"?>
<!--
/**
 * Copyright ©2020 coddu.com, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Api\ProductRepositoryInterface">
        <plugin name="BrandProductExtensionAttribute"
                type="Coddu\ProductExtAttribute\Plugin\Product\GetExtAttributesPlugin"/>
    </type>

</config>

Step 6:

As I declared a plugin class in ‘di.xml’ file, I am creating a before plugin for ‘Get’ method in ‘GetExtAttributesPlugin’ class. First, I will get all extension attributes and set the ‘brand’ attribute to extension attribute.

<?php

/**
 * Copyright ©2020 coddu.com, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Coddu\ProductExtAttribute\Plugin\Product;

use Magento\Catalog\Api\Data\ProductInterfaceFactory;
use Magento\Catalog\Api\Data\ProductExtensionFactory;
use Magento\Catalog\Api\ProductRepositoryInterface;

class GetExtAttributesPlugin
{
    private $_productExtFactory;

    /**
     * GetExtAttributes constructor.
     * @param ProductExtensionFactory $productExtFactory
     */
    function __construct(ProductExtensionFactory $productExtFactory)
    {
        $this->_productExtFactory = $productExtFactory;
    }

    /**
     * @param ProductRepositoryInterface $productRepository
     * @param $result
     * @return mixed
     */
    public function afterGet(ProductRepositoryInterface $productRepository, $result)
    {

        $extensionAttributes = $result->getExtensionAttributes();
        $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->_productExtFactory->create();

        $attrCode = 'brand';
        $attr = $result->getCustomAttribute($attrCode);
        if ($attr) {
            $extensionAttributes->setData($attrCode, $attr->getValue());
        }

        $result->setExtensionAttributes($extensionAttributes);

        return $result;
    }
}

Result:

This is the time to see the result, I updated a product record and hit the rest api url ‘http://localhost:8080/index.php/rest/V1/products/24-MB01’.

{
	"attribute_set_id": 15,
	"created_at": "2020-10-07 16:09:53",
	"custom_attributes": [
		....
		{
			"attribute_code": "brand",
			"value": "https://coddu.com"
		}
		....		
	],
	"extension_attributes": {
		"website_ids": [
			1
		],
		....
		"category_links": [
			{
				"position": 0,
				"category_id": "3"
			},
			{
				"position": 0,
				"category_id": "4"
			}
		],
		....
		"brand": "https://coddu.com"
	},
	"id": 1,
	"name": "Joust Duffle Bag",
	"options": [],
	"price": 34,
	....
	"sku": "24-MB01",
	"status": 1,
	"tier_prices": [],
	"type_id": "simple",
	"updated_at": "2020-10-07 16:21:17",
	"visibility": 4
}

If you have any question, please ask in comment section.

Thank you!