How to assign product attribute to attribute set Magento 2?

Product attributes will be displayed in the product admin page by assigning an attribute to specific attribute set groups from the attribute set section in the backend.

You can manually set an attribute to the attribute group by click on Left sidebar, Stores -> Attributes ->Attribute Set.
Click on Any attribute set and drag and drop attribute from the Unassigned Attributes section to Groups section.

We can assign an attribute to attribute set programmatically using the interface, Magento\Catalog\Api\ProductAttributeManagementInterface

Base Definition of the method to assign attribute,

 public function assign($attributeSetId, $attributeGroupId, $attributeCode, $sortOrder);

$attributeSetId indicate Attribute set id.
$attributeGroupId indicate Attribute group id.
$attributeCode indicates the attribute code you want to assign to a specific attribute group.
$sortOrder indicates the sort order of the attribute in the group from the list of the attribute of the same attribute group.

<?php
namespace Jesadiya\AssignAttribute\Model;

use Magento\Framework\Exception\LocalizedException;
use Magento\Catalog\Api\ProductAttributeManagementInterface;

class AssignAttributeSet
{
    /**
     * @var ProductAttributeManagementInterface
     */
    private $productAttributeManagement;

    public function __construct(
        ProductAttributeManagementInterface $productAttributeManagement
    ) {
        $this->productAttributeManagement = $productAttributeManagement;
    }

    /**
     * Assign Product Attribute to attribute Set Group.
     *
     * @return int
     */
    public function assignAttributeToGroup()
    {
        $attributeSetId = 16;
        $attributeGroupId = 91;
        $attributeCode = 'size';
        $sortOrder = 50;
        try {
            $productAttributeAssignment = $this->productAttributeManagement
                ->assign($attributeSetId, $attributeGroupId, $attributeCode, $sortOrder);
        } catch (LocalizedException $exception) {
            throw new LocalizedException(__($exception->getMessage()));
        }

        return $productAttributeAssignment;
    }
}

You need to pass the required parameter in the above method to assign the attribute to your required attribute group.
Call from the template or any PHP class,

$assignAttributeToGroupId = $this->assignAttributeToGroup();
attribute assign
Attribute assign to attribute set

Output:
int on assigned attribute successful.